package com.yishusmt.wechatapi.wechatdev.controller;

import com.yishusmt.wechatapi.wechatdev.entity.button.BaseButtonEntity;
import com.yishusmt.wechatapi.wechatdev.utils.JacksonUtil;
import com.yishusmt.wechatapi.wechatdev.utils.MenuUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * 自定义创建菜单接口
 *
 * @author zhangyi
 * @date 2018/9/1 11:14
 */
@Controller
@RequestMapping("/menu")
@Api(description = "菜单操作")
public class MenuOperateController {

    @Autowired
    private MenuUtil menuUtil;

    /**
     * 创建菜单
     *
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @GetMapping("/createMenu")
    @ResponseBody
    @ApiOperation(value = "创建菜单，无需传参，但需要先修改配置文件(menu.json)",httpMethod = "GET")
    public String menu() throws IOException, URISyntaxException {
        String menu = menuUtil.createMenu();
        return menu;
    }

    /**
     * 获取菜单配置文件
     *
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @GetMapping("/getMenu")
    @ResponseBody
    @ApiOperation(value = "获取菜单文件menu.json",httpMethod = "GET")
    public String getMenu() throws IOException{
        String menu = menuUtil.getMenuFile();
        return menu;
    }

    /**
     * 修改菜单文件
     *
     * @param menuResource
     * @return
     * @throws IOException
     */
    @PostMapping("/editMenu")
    @ResponseBody
    @ApiOperation(value = "修改菜单文件menu.json",httpMethod = "POST")
    public String editMenu(String menuResource) throws IOException{
        BaseButtonEntity baseButtonEntity =
                JacksonUtil.readValue(menuResource, BaseButtonEntity.class);
        String s = JacksonUtil.toJSon(baseButtonEntity);
        menuUtil.editMenuFile(menuResource);
        return s;
    }
}
